package com.flight.trans.fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.adapter.LeavePorReturnAdapter;
import com.flight.trans.bean.LeavePortReturnBean.LeavePortReturnData;
import com.flight.trans.global.Constant;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.util.ChinaToPinyin;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ProgressUtils;
import com.flight.trans.view.dialog.AlertDialog;
import com.flight.trans.view.HeaderTitleView;
import com.flight.trans.base.BaseFragment;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 出港退库
 */
public class LeavePortReturnFragment extends BaseFragment {

    private HeaderTitleView mHeaderView ;

    private TwinklingRefreshLayout mRefreshLayout ;
    private RecyclerView mRvContainer ;
    private RelativeLayout mRlContentNull ;

    private TextView mTvOrderNo,//运单号
            mTvProductName ,//品名
            mTvDeep ,//目的地
            mTvReturnCount ,//退运件数
            mTvReturnWeight ,//退运重量
            mTvStorageArea ,//存放区域
            mTvSender ,//发货人
            mTvWarehousingTime ;//入库时间

    private LeavePorReturnAdapter mReturnAdapter;
    private List<LeavePortReturnData> mReturnDataList,mSearchDataList;

    private int mSortState = -1;

    @Override
    public int setContentView() {
        return R.layout.fragment_leave_port_return;
    }

    @Override
    public void initView() {
        mHeaderView = mView.findViewById(R.id.mHeaderView);
        mRefreshLayout = mView.findViewById(R.id.mRefreshLayout);
        mRvContainer = mView.findViewById(R.id.mRvContainer);
        mRlContentNull = mView.findViewById(R.id.mRlContentNull);

        mTvOrderNo = mView.findViewById(R.id.mTvOrderNo);
        mTvProductName = mView.findViewById(R.id.mTvProductName);
        mTvDeep = mView.findViewById(R.id.mTvDeep);
        mTvReturnCount = mView.findViewById(R.id.mTvReturnCount);
        mTvReturnWeight = mView.findViewById(R.id.mTvReturnWeight);
        mTvStorageArea = mView.findViewById(R.id.mTvStorageArea);
        mTvSender = mView.findViewById(R.id.mTvSender);
        mTvWarehousingTime = mView.findViewById(R.id.mTvWarehousingTime);
    }

    @Override
    public void initData() {
        initHeaderView();

        mTvOrderNo.setOnClickListener(this::onClickSort);
        mTvProductName.setOnClickListener(this::onClickSort);
        mTvDeep.setOnClickListener(this::onClickSort);
        mTvReturnCount.setOnClickListener(this::onClickSort);
        mTvReturnWeight.setOnClickListener(this::onClickSort);
        mTvStorageArea.setOnClickListener(this::onClickSort);
        mTvSender.setOnClickListener(this::onClickSort);
        mTvWarehousingTime.setOnClickListener(this::onClickSort);

        CommonUtils.setScreenViewVisible(mTvSender);
        CommonUtils.setScreenViewVisible(mTvWarehousingTime);

        setRefreshListener(mRefreshLayout);
    }

    @Override
    public void initConnect() {
        if(mHeaderView == null) return;

        valueParams = new HttpParams();
        valueParams.put(Constant.Key_Done , mHeaderView.isDone());
        if(mHeaderView.isDone())
            valueParams.put(Constant.Key_Returned_Date , mHeaderView.getSelectDate());

        NetworkUtils.getLeavePortReturnList(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mReturnDataList = (List<LeavePortReturnData>) body;

                onScanResult(mHeaderView.getSearchContent());
            }
        }, valueParams);

    }

    private void initAdapter(){
        mRlContentNull.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.VISIBLE : View.GONE);
        mRefreshLayout.setVisibility(mSearchDataList == null || mSearchDataList.size() == 0 ? View.GONE : View.VISIBLE);

        if(mReturnAdapter == null) {
            mReturnAdapter = new LeavePorReturnAdapter(mActivity, mSearchDataList, !mHeaderView.isDone());
            mRvContainer.setAdapter(mReturnAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mReturnAdapter.setOnItemClickListener(new ReturnItemOnClickListener());
        }else {
            mReturnAdapter.updateAdapter(mSearchDataList, !mHeaderView.isDone());
        }
    }

    /**
     * 点击事件
     */
    private class ReturnItemOnClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            if(mHeaderView.isDone())
                return;

            AlertDialog.getInstance(mActivity)
                    .setContent(R.string.dialog_leave_port_return)
                    .setConfirmButton(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            valueParams = new HttpParams();
                            valueParams.put(Constant.RETURNED_ID , mSearchDataList.get(position).getReturnedId());
                            NetworkUtils.saveLeavePortReturn(new OkGoBackListener(mActivity) {
                                @Override
                                public void onSuccess(Object body) {
                                    initConnect();
                                }
                            }, valueParams);
                        }
                    }).show();
        }
    }

    private void initHeaderView() {
        mHeaderView.addOnHeaderListener(new HeaderTitleView.OnHeaderListener(){
            @Override
            public void onSearch(String search) {
                onScanResult(search);
            }

            @Override
            public void onPartChange(int isChecked) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }

            @Override
            public void onDateChange(String date) {
                ProgressUtils.showProgress(mActivity);
                initConnect();
            }
        });
    }

    @Override
    public void onScanResult(String code) {
        mHeaderView.setSearchContent(code);

        mSearchDataList = new ArrayList<>();

        if(!TextUtils.isEmpty(code)) {
            for (int i = 0; i < mReturnDataList.size(); i++)
                if(mReturnDataList.get(i).getOrderNo() != null && mReturnDataList.get(i).getOrderNo().contains(code))
                    mSearchDataList.add(mReturnDataList.get(i));
        }else {
            mSearchDataList.addAll(mReturnDataList) ;
        }

        if(mSearchDataList != null)
            if(mSortState == 0){
                Collections.reverse(mSearchDataList);
            }else if(mSortState != -1){
                Collections.sort(mSearchDataList, new SortComparator(mSortState));
            }

        initAdapter();
    }

    public void onClickSort(View view){
        mSortState = (mSortState != 0 && R.id.mTvOrderNo == view.getId()) ? 0 : (mSortState == 0 ? -1 : mSortState) ;
        mSortState = (mSortState != 1 && R.id.mTvProductName == view.getId()) ? 1 : mSortState == 1 ? -1 : mSortState ;
        mSortState = (mSortState != 2 && R.id.mTvDeep == view.getId()) ? 2 : mSortState == 2 ? -1 : mSortState ;
        mSortState = (mSortState != 3 && R.id.mTvReturnCount == view.getId()) ? 3 : mSortState == 3 ? -1 : mSortState ;
        mSortState = (mSortState != 4 && R.id.mTvReturnWeight == view.getId()) ? 4 : mSortState == 4 ? -1 : mSortState ;
        mSortState = (mSortState != 5 && R.id.mTvStorageArea == view.getId()) ? 5 : mSortState == 5 ? -1 : mSortState ;
        mSortState = (mSortState != 6 && R.id.mTvSender == view.getId()) ? 6 : mSortState == 6 ? -1 : mSortState ;
        mSortState = (mSortState != 7 && R.id.mTvWarehousingTime == view.getId()) ? 7 : mSortState == 7 ? -1 : mSortState ;

        mTvOrderNo.setTextColor(mActivity.getColor(mSortState == 0 ? R.color.color_red : R.color.color_blue));
        mTvProductName.setTextColor(mActivity.getColor(mSortState == 1 ? R.color.color_red : R.color.color_blue));
        mTvDeep.setTextColor(mActivity.getColor(mSortState == 2 ? R.color.color_red : R.color.color_blue));
        mTvReturnCount.setTextColor(mActivity.getColor(mSortState == 3 ? R.color.color_red : R.color.color_blue));
        mTvReturnWeight.setTextColor(mActivity.getColor(mSortState == 4 ? R.color.color_red : R.color.color_blue));
        mTvStorageArea.setTextColor(mActivity.getColor(mSortState == 5 ? R.color.color_red : R.color.color_blue));
        mTvSender.setTextColor(mActivity.getColor(mSortState == 6 ? R.color.color_red : R.color.color_blue));
        mTvWarehousingTime.setTextColor(mActivity.getColor(mSortState == 7 ? R.color.color_red : R.color.color_blue));

        onScanResult(mHeaderView.getSearchContent());
    }

    /**
     * 排序集合
     */
    private class SortComparator implements Comparator<LeavePortReturnData> {

        private int type;

        public SortComparator(int type){
            this.type = type;
        }

        @Override
        public int compare(LeavePortReturnData data1, LeavePortReturnData data2) {
            switch (type){
                case 1 :
                    return ChinaToPinyin.getInstance().sort(data1.getTradeName() , data2.getTradeName());
                case 2 :
                    return ChinaToPinyin.getInstance().sort(data1.getDeap() , data2.getDeap());
                case 3 :
                    return data2.getNum() - data1.getNum();
                case 4 :
                    return (int)(data2.getRealWeight() - data1.getRealWeight());
                case 5 :
                    return data2.getWarehouseAreaId() - data1.getWarehouseAreaId();
                case 6 :
                    return ChinaToPinyin.getInstance().sort(data1.getSenderName() , data2.getSenderName());
                case 7 :
                    return (int) (CommonUtils.getTimeLong(data2.getInstorageTime()) - CommonUtils.getTimeLong(data1.getInstorageTime()));
                default:
                    return 0 ;
            }
        }
    }
}
